#include "CameraParametersWidget.h"
#include "ui_CameraParametersWidget.h"
#include "VisiyunDevice.h"

CameraParametersWidget::CameraParametersWidget(VisiyunDevice *device, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CameraParametersWidget),
    m_device(device)
{
    ui->setupUi(this);

    // 初始化信号槽连接
    connect(ui->exposureSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_exposureSlider_valueChanged);
    connect(ui->frequencySlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_frequencySlider_valueChanged);
    connect(ui->gainSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_gainSlider_valueChanged);
    connect(ui->exposureSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_exposureSpinBox_valueChanged);
    connect(ui->frequencySpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_frequencySpinBox_valueChanged);
    connect(ui->gainSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_gainSpinBox_valueChanged);

    if (m_device && m_device->isConnected()) 
    {
        // 从设备获取当前值
        quint64 currentExposure = m_device->getExposureTime(); // 纳秒
        // 转换为毫秒显示
        setExposureTime(currentExposure / 1000000);
        
        // 从设备获取曝光时间范围
        quint64 minExposure = m_device->getMinExposureTime();
        quint64 maxExposure = m_device->getMaxExposureTime();
        setExposureRange(minExposure / 1000000, maxExposure / 1000000);
        
        // 从设备获取当前采样频率
        quint64 currentFrequency = m_device->getSamplingFrequency();
        setSamplingFrequency(currentFrequency/100);
        
        // 从设备获取采样频率范围
        quint64 minFrequency = m_device->getMinFrequency();
        quint64 maxFrequency = m_device->getMaxFrequency();
        setFrequencyRange(minFrequency/100, maxFrequency/100);
        
        // 从设备获取当前增益值
        quint64 currentGain = m_device->getGain();
        setGain(currentGain);
        
        // 从设备获取增益范围
        quint64 minGain = m_device->getMinGain();
        quint64 maxGain = m_device->getMaxGain();
        setGainRange(minGain, maxGain);
    } 
    else 
    {
        // 如果设备未连接，使用安全的默认值
        setExposureTime(100);
        setSamplingFrequency(10);
        setGain(0);
        setExposureRange(1, 10000);
        setFrequencyRange(1, 100);
        setGainRange(0, 100);
    }
}

void CameraParametersWidget::setDevice(VisiyunDevice *device)
{
    m_device = device;
}

CameraParametersWidget::~CameraParametersWidget()
{
    delete ui;
}

void CameraParametersWidget::setExposureTime(int value)
{
    // 断开信号连接，避免循环触发
    disconnect(ui->exposureSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_exposureSlider_valueChanged);
    disconnect(ui->exposureSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_exposureSpinBox_valueChanged);

    ui->exposureSlider->setValue(value);
    ui->exposureSpinBox->setValue(value);

    // 重新连接信号
    connect(ui->exposureSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_exposureSlider_valueChanged);
    connect(ui->exposureSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_exposureSpinBox_valueChanged);
}

void CameraParametersWidget::setSamplingFrequency(int value)
{
    // 断开信号连接，避免循环触发
    disconnect(ui->frequencySlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_frequencySlider_valueChanged);
    disconnect(ui->frequencySpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_frequencySpinBox_valueChanged);

    ui->frequencySlider->setValue(value);
    ui->frequencySpinBox->setValue(value);

    // 重新连接信号
    connect(ui->frequencySlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_frequencySlider_valueChanged);
    connect(ui->frequencySpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_frequencySpinBox_valueChanged);
}

void CameraParametersWidget::setGain(int value)
{
    // 断开信号连接，避免循环触发
    disconnect(ui->gainSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_gainSlider_valueChanged);
    disconnect(ui->gainSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_gainSpinBox_valueChanged);

    ui->gainSlider->setValue(value);
    ui->gainSpinBox->setValue(value);

    // 重新连接信号
    connect(ui->gainSlider, &QSlider::valueChanged, this, &CameraParametersWidget::on_gainSlider_valueChanged);
    connect(ui->gainSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &CameraParametersWidget::on_gainSpinBox_valueChanged);
}

int CameraParametersWidget::getExposureTime() const
{
    return ui->exposureSlider->value();
}

int CameraParametersWidget::getSamplingFrequency() const
{
    return ui->frequencySlider->value();
}

int CameraParametersWidget::getGain() const
{
    return ui->gainSlider->value();
}

void CameraParametersWidget::setExposureRange(int min, int max)
{
    ui->exposureSlider->setRange(min, max);
    ui->exposureSpinBox->setRange(min, max);
}

void CameraParametersWidget::setFrequencyRange(int min, int max)
{
    ui->frequencySlider->setRange(min, max);
    ui->frequencySpinBox->setRange(min, max);
}

void CameraParametersWidget::setGainRange(int min, int max)
{
    ui->gainSlider->setRange(min, max);
    ui->gainSpinBox->setRange(min, max);
}

void CameraParametersWidget::on_exposureSlider_valueChanged(int value)
{
    setExposureTime(value);
    // 直接调用设备方法设置曝光时间
    if (m_device) {
        m_device->setExposureTime(static_cast<quint64>(value) * 1000000); // 毫秒转换为纳秒
    }
}

void CameraParametersWidget::on_frequencySlider_valueChanged(int value)
{
    setSamplingFrequency(value);
    // 直接调用设备方法设置采样频率
    if (m_device) {
        m_device->setSamplingFrequency(static_cast<quint64>(value*100));
    }
}

void CameraParametersWidget::on_gainSlider_valueChanged(int value)
{
    setGain(value);
    // 直接调用设备方法设置增益
    if (m_device) {
        m_device->setGain(static_cast<quint64>(value));
    }
}

void CameraParametersWidget::on_exposureSpinBox_valueChanged(int value)
{
    setExposureTime(value);
    // 直接调用设备方法设置曝光时间
    if (m_device) {
        m_device->setExposureTime(static_cast<quint64>(value) * 1000000); // 毫秒转换为纳秒
    }
}

void CameraParametersWidget::on_frequencySpinBox_valueChanged(int value)
{
    setSamplingFrequency(value);
    // 直接调用设备方法设置采样频率
    if (m_device) {
        m_device->setSamplingFrequency(static_cast<quint64>(value));
    }
}

void CameraParametersWidget::on_gainSpinBox_valueChanged(int value)
{
    setGain(value);
    // 直接调用设备方法设置增益
    if (m_device) {
        m_device->setGain(static_cast<quint64>(value));
    }
}